// by Xeno
#define THIS_FILE "kbinit.sqf"
#include "x_setup.sqf"
private ["_grpen", "_grpru", "_kbscript", "_pside"];

if (isServer) then {
	_grpen = [blufor] call FUNC(creategroup);
	GVAR(hq_logic_en1) = _grpen createUnit ["Logic",[0,0,0],[],0,"NONE"];
	[GVAR(hq_logic_en1)] joinSilent _grpen;
	GVAR(hq_logic_en1) enableSimulation false;
	GVAR(hq_logic_en1) hideObject true;
	GVAR(hq_logic_en1) addItem "ItemRadio";
	GVAR(hq_logic_en1) assignItem "ItemRadio";
	publicVariable QGVAR(hq_logic_en1);
	GVAR(hq_logic_en1) setVariable [QGVAR(hq_logic_name), QGVAR(hq_logic_en1)];
	GVAR(hq_logic_en1) setVariable [QGVAR(hq_logic_side), blufor];
	GVAR(hq_logic_en1) addEventHandler ["killed",{_this call FUNC(kEHflogic)}];
	GVAR(hq_logic_en1) setVariable [QGVAR(noSimCheck), true];
	
	_grpen = [blufor] call FUNC(creategroup);
	GVAR(hq_logic_en2) = _grpen createUnit ["Logic",[0,0,0],[],0,"NONE"];
	[GVAR(hq_logic_en2)] joinSilent _grpen;
	GVAR(hq_logic_en2) addEventHandler ["handleDamage",{0}];
	GVAR(hq_logic_en2) enableSimulation false;
	GVAR(hq_logic_en2) hideObject true;
	GVAR(hq_logic_en2) addItem "ItemRadio";
	GVAR(hq_logic_en2) assignItem "ItemRadio";
	publicVariable QGVAR(hq_logic_en2);
	GVAR(hq_logic_en2) setVariable [QGVAR(hq_logic_name), QGVAR(hq_logic_en2)];
	GVAR(hq_logic_en2) setVariable [QGVAR(hq_logic_side), blufor];
	GVAR(hq_logic_en2) addEventHandler ["killed",{_this call FUNC(kEHflogic)}];
	GVAR(hq_logic_en2) setVariable [QGVAR(noSimCheck), true];

	_grpru = [opfor] call FUNC(creategroup);
	GVAR(hq_logic_ru1) = _grpru createUnit ["Logic",[0,0,0],[],0,"NONE"];
	[GVAR(hq_logic_ru1)] joinSilent _grpru;
	GVAR(hq_logic_ru1) enableSimulation false;
	GVAR(hq_logic_ru1) hideObject true;
	GVAR(hq_logic_ru1) addEventHandler ["handleDamage",{0}];
	GVAR(hq_logic_ru1) addItem "ItemRadio";
	GVAR(hq_logic_ru1) assignItem "ItemRadio";
	publicVariable QGVAR(hq_logic_ru1);
	GVAR(hq_logic_ru1) setVariable [QGVAR(hq_logic_name), QGVAR(hq_logic_ru1)];
	GVAR(hq_logic_ru1) setVariable [QGVAR(hq_logic_side), opfor];
	GVAR(hq_logic_ru1) addEventHandler ["killed",{_this call FUNC(kEHflogic)}];
	GVAR(hq_logic_ru1) setVariable [QGVAR(noSimCheck), true];
	
	_grpru = [opfor] call FUNC(creategroup);
	GVAR(hq_logic_ru2) = _grpru createUnit ["Logic",[0,0,0],[],0,"NONE"];
	[GVAR(hq_logic_ru2)] joinSilent _grpru;
	GVAR(hq_logic_ru2) enableSimulation false;
	GVAR(hq_logic_ru2) hideObject true;
	GVAR(hq_logic_ru2) addItem "ItemRadio";
	GVAR(hq_logic_ru2) assignItem "ItemRadio";
	GVAR(hq_logic_ru2) addEventHandler ["handleDamage",{0}];
	publicVariable QGVAR(hq_logic_ru2);
	GVAR(hq_logic_ru2) setVariable [QGVAR(hq_logic_name), QGVAR(hq_logic_ru2)];
	GVAR(hq_logic_ru2) setVariable [QGVAR(hq_logic_side), opfor];
	GVAR(hq_logic_ru2) addEventHandler ["killed",{_this call FUNC(kEHflogic)}];
	GVAR(hq_logic_ru2) setVariable [QGVAR(noSimCheck), true];
} else {
	waitUntil {!isNil QGVAR(hq_logic_ru2)};
	{
		_lo = missionNamespace getVariable _x;
		_lo enableSimulation false;
		_lo hideObject true;
		_lo setVariable [QGVAR(noSimCheck), true];
	} forEach [QGVAR(hq_logic_en1), QGVAR(hq_logic_en2), QGVAR(hq_logic_ru1), QGVAR(hq_logic_ru2)];
};

_kbscript = "x_bikb\domkba3.bikb";

GVAR(hq_logic_ru1) kbAddTopic["HQ_E",_kbscript];
GVAR(hq_logic_ru1) kbAddTopic["HQ_ART_E",_kbscript];
GVAR(hq_logic_ru1) setIdentity "DHQ_RU1";
GVAR(hq_logic_ru1) setRank "COLONEL";
GVAR(hq_logic_ru1) setGroupId ["HQ"];
GVAR(hq_logic_ru1) setVariable [QGVAR(kddata), [["HQ_E", "HQ_ART_E"], "DHQ_RU1","HQ"]];

GVAR(hq_logic_ru2) kbAddTopic["HQ_E",_kbscript];
GVAR(hq_logic_ru2) setIdentity "DHQ_RU2";
GVAR(hq_logic_ru2) setRank "COLONEL";
GVAR(hq_logic_ru2) setGroupId ["HQ1"];
GVAR(hq_logic_ru2) setVariable [QGVAR(kddata), [["HQ_E"], "DHQ_RU2","HQ1"]];

GVAR(hq_logic_en1) kbAddTopic["HQ_W",_kbscript];
GVAR(hq_logic_en1) kbAddTopic["HQ_ART_W",_kbscript];
GVAR(hq_logic_en1) setIdentity "DHQ_EN1";
GVAR(hq_logic_en1) setRank "COLONEL";
GVAR(hq_logic_en1) setGroupId ["Crossroad"];
GVAR(hq_logic_en1) setVariable [QGVAR(kddata), [["HQ_W", "HQ_ART_W"], "DHQ_EN1","Crossroad"]];

GVAR(hq_logic_en2) kbAddTopic["HQ_W",_kbscript];
GVAR(hq_logic_en2) setIdentity "DHQ_EN2";
GVAR(hq_logic_en2) setRank "COLONEL";
GVAR(hq_logic_en2) setGroupId ["Crossroad1"];
GVAR(hq_logic_en2) setVariable [QGVAR(kddata), [["HQ_W"], "DHQ_EN2","Crossroad1"]];

#ifndef __TT__
GVAR(kb_logic1) = switch (GVAR(enemy_side)) do {
	case "OPFOR": {GVAR(hq_logic_en1)};
	case "BLUFOR": {GVAR(hq_logic_ru1)};
};
GVAR(kb_logic2) = switch (GVAR(enemy_side)) do {
	case "OPFOR": {GVAR(hq_logic_en2)};
	case "BLUFOR": {GVAR(hq_logic_ru2)};
};
GVAR(kb_topic_side) = switch (GVAR(enemy_side)) do {
	case "OPFOR": {"HQ_W"};
	case "BLUFOR": {"HQ_E"};
};
GVAR(kb_topic_side_arti) = switch (GVAR(enemy_side)) do {
	case "OPFOR": {"HQ_ART_W"};
	case "BLUFOR": {"HQ_ART_E"};
};
#endif

if (!isDedicated) then {
	sleep 1;
	if (isMultiplayer) then {
		waitUntil {sleep 0.220;!isNil QGVAR(still_in_intro)};
		waitUntil {sleep 0.220;!GVAR(still_in_intro)};
	};
	_pside = side (group player);
	switch (_pside) do {
		case blufor: {player kbAddTopic["HQ_W",_kbscript]};
		case opfor: {player kbAddTopic["HQ_E",_kbscript]};
	};
	_strp = str(player);
	player kbAddTopic["PL" + _strp,_kbscript];
	#ifndef __TT__
	GVAR(kb_logic1) kbAddTopic["PL" + _strp,_kbscript];
	#else
	_ll = if (_pside == opfor) then {GVAR(hq_logic_ru1)} else {GVAR(hq_logic_en1)};
	_ll kbAddTopic["PL" + _strp,_kbscript];
	#endif
	if (!GVAR(with_ai) && {GVAR(with_ai_features) != 0}) then {
		if (_strp in GVAR(can_use_artillery)) then {
			switch (_pside) do {
				case blufor: {player kbAddTopic["HQ_ART_W",_kbscript]};
				case opfor: {player kbAddTopic["HQ_ART_E",_kbscript]};
			};
		};
	} else {
		switch (_pside) do {
			case blufor: {player kbAddTopic["HQ_ART_W",_kbscript]};
			case opfor: {player kbAddTopic["HQ_ART_E",_kbscript]};
		};
	};
};

